package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.GasStationInfo;
import com.sgb.kjwl.model.entity.GasStationInfoEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.AddGasStationActivity;

import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class StationModel extends BaseViewModel {
    private SelfRepertory repertory;
    private AddGasStationActivity activity;
    public ObservableField<GasStationInfo> stationInfo = new ObservableField<>(new GasStationInfo());
    public StationModel(SelfRepertory repertory, AddGasStationActivity activity) {
        this.repertory = repertory;
        this.activity = activity;
    }
    public void editeInfo(){
        DialogHelper.showProgressDialog(activity, null, "提交中...", 0, true, null).setCanceledOnTouchOutside(false);

        APIManager.getInstance().getSelfOwnerAPI().editStation(stationInfo.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity,"提交成功");
                   activity.finish();
                }else{
                    MToast.showToast(activity,response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getStationInfo(){
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getStationInfo().enqueue(new Callback<BaseEntity<GasStationInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<GasStationInfoEntity>> call, Response<BaseEntity<GasStationInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    if(response.body().getData()!=null) {
                        activity.getInfo(response.body().getData());
                    }
                }else{
                    MToast.showToast(activity,response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<GasStationInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //上传多图片
    public void uploadMutiFileModel(File file, int type) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" +  "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.uploadRefresh(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    //区域
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
